package com.androturka;

import java.io.IOException;
import java.text.ParseException;
import org.apache.http.client.ClientProtocolException;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.provider.Settings.Secure;
import android.util.Log;
import com.google.android.c2dm.C2DMBaseReceiver;

public class C2DMReceiver extends C2DMBaseReceiver {
	
	ConfigurationManager configurationManager;

	public C2DMReceiver() {
		// Email address currently not used by the C2DM Messaging framework
		super("dummy@google.com");
	}

	@Override
	public void onRegistered(Context context, String registrationId)
			throws java.io.IOException {
		
		String deviceId = Secure.getString(context.getContentResolver(),
				Secure.ANDROID_ID);
		Log.d("C2DM", "Registration ID received : " + registrationId);
		Log.d("C2DM", "Device ID : " + deviceId);
		sendRegistrationIdToServer(deviceId, registrationId);
		saveRegistrationId(context, registrationId);
	};

	private void saveRegistrationId(Context context, String registrationId) {
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(context);
		Editor edit = prefs.edit();
		edit.putString("REGISTRATIONID", registrationId);
		edit.commit();
	}	
	@Override
	protected void onMessage(Context context, Intent intent) {		
		Log.d("C2DM", "New Message Received");
		try {
			handleData(context,intent);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}

	@Override
	public void onError(Context context, String errorId) {
		Log.e("C2DM", "Error occured!!!");
		Log.e("C2DM", errorId);
	}

	public void handleData(Context context, Intent messageIntent ) throws ParseException {

		Log.d("C2DM", "New Earthquake Info Received");

		String date =  messageIntent.getStringExtra("date");
	    String latitude =  messageIntent.getStringExtra("latitude");
	    String longitude =  messageIntent.getStringExtra("longitude");
	    String depth =  messageIntent.getStringExtra("depth");		
	    String magnitude =  messageIntent.getStringExtra("magnitude");		
		String location =  messageIntent.getStringExtra("location");
	    
		if ( ( date != null ) &&
			 ( latitude != null ) &&
			 ( longitude != null ) &&
			 ( depth != null ) &&
			 ( magnitude != null ) &&
			 ( location != null ) )	
		{	
		    Log.d("C2DM", "date : " + date);
		    Log.d("C2DM", "latitude : " + latitude);
		    Log.d("C2DM", "longitude : " + longitude);
		    Log.d("C2DM", "depth : " + depth);
		    Log.d("C2DM", "magnitude : " + magnitude);
		    Log.d("C2DM", "location : " + location);
	
		    EarthQuakeNotification EQN = new EarthQuakeNotification(context);		
		    EQN.displayNotification(WebAdapter.getDate(date),Double.parseDouble(latitude),Double.parseDouble(longitude),Double.parseDouble(depth),Double.parseDouble(magnitude),location);
		}
		else
		{	
			Log.e("C2DM","Missing Parameters");
			startService(new Intent(this, ClientService.class));
		}	
	}	

	public void sendRegistrationIdToServer(String deviceId,
			String registrationId) throws ClientProtocolException, IOException {
		Log.d("C2DM", "Sending registration ID to my application server");
		
		ConfigurationSender sender = new ConfigurationSender(getApplicationContext());
		sender.sendConfiguration();		
	}
}
